from configobj import ConfigObj
import eventManager

# 如果需要对话框回调，需要初始化加入eventManager对象

class MyConfig(object):
    '''
    * 配置文件读取
    * 系统配置： 语言类型
    * 程序配置
    * 错误提示配置
    * 读取参数： 文件名，节点1，节点2
    '''

    def __init__(self,event):
        self.event = event
        pass

    def read_file(filename):
        readout = ConfigObj(filename+".ini")
        return readout

    def read_file_node(filename,node):
        readout = ConfigObj(filename+".ini")
        return readout[node]

    def read_file_node_nnode(filename,node,nnode):
        readout = ConfigObj(filename+".ini")
        return readout[node][nnode]

    def get_config_1(self,filename,list):
        '''
        * 依据list配置取出文件中的参数, 取第一级中的配置
        * list格式举例  list=["Cavity","FileDir","Plot"]
        '''
        result = self.read_file(filename)
        if len(list) == 0:
            return None

        for key,value in result.items():
            if key in list:
                pass
            else:
                del result[key]
        return result
        pass

    def get_config_2(self,filename,node,list):
        '''
        * 依据list配置取出文件中的参数，取第二级中的配置
        '''
        result = self.read_file(filename,node)
        if len(list) == 0:
            return None

        for key,value in result.items():
            if key in list:
                pass
            else:
                del result[key]
        return result
        pass


    def get_node(self,data,node):
        """
        @description  : 从data获取特定节点配置 ，未获得报错
        @param  : 
        @Returns  :
        """
        if node in data:
            return data[node]
        else:
            return None

    def readConfig(self):
        # 读取配置文件
        conf_ini = "config.ini"
        self.config = ConfigObj(conf_ini)
        sample = {
            "Cavity": "没有配置积分腔参数！",
            "FileDir": "配置文件路径没有配置！",
            "Plot": "绘图参数您没有配置！",
            "ComSetting": "配置文件没有配置串口参数！",
            "LogSetting": "配置文件没有配置采集时间间隔！",
            "Line": "配置文件没有配置读取文件的开始行和结束行！",
            "SNCommand": "配置文件没有配置串口读取波长命令！",
            "WLCommand": "配置文件没有配置串口读取波长命令！",
            "SignalCommand": "配置文件没有配置串口读取光强的命令！"
        }

        for key,value in sample.items():
            if key in self.config:
                setattr(self,key,self.config[key])
                pass
            else:
                with wx.MessageDialog(self, value, u"错误提示", wx.OK )as dlg:
                    dlg.ShowModal()        

    def readConfig2 (self):
        # 读取配置文件
        conf_ini = "config.ini"
        config = ConfigObj(conf_ini)
        if 'Cavity' in config:
            self.Cavity = config['Cavity']
        else:
            with wx.MessageDialog(self, u"没有配置积分腔参数！", u"错误提示", wx.OK )as dlg:
                dlg.ShowModal()

        if 'FileDir' in config:
            self.FileDir = config['FileDir']
        else:
            with wx.MessageDialog(self, u"配置文件路径没有配置！", u"错误提示", wx.OK )as dlg:
                dlg.ShowModal()

        if 'Plot' in config:
            self.Plot = config['Plot']
        else:
            with wx.MessageDialog(self, u"绘图参数您没有配置！", u"错误提示", wx.OK )as dlg:
                dlg.ShowModal()

        if 'ComSetting' in config:
            self.ComSetting = config['ComSetting']
        else:
            with wx.MessageDialog(self, u"配置文件没有配置串口参数！", u"错误提示", wx.OK )as dlg:
                dlg.ShowModal()

        if 'LogSetting' in config:
            self.LogSetting  = config['LogSetting']
        else:
            with wx.MessageDialog(self, u"配置文件没有配置串口参数！", u"错误提示", wx.OK )as dlg:
                dlg.ShowModal()

        if 'Line' in config:
            self.Line  = config['Line']
        else:
            with wx.MessageDialog(self, u"配置文件没有配置读取文件的开始行和结束行！", u"错误提示", wx.OK )as dlg:
                dlg.ShowModal()

        if 'SNCommand' in config:
            self.SNCommand  = config['SNCommand']
        else:
            with wx.MessageDialog(self, u"配置文件没有配置串口读取波长命令！", u"错误提示", wx.OK )as dlg:
                dlg.ShowModal()

        if 'WLCommand' in config:
            self.WLCommand  = config['WLCommand']
        else:
            with wx.MessageDialog(self, u"配置文件没有配置串口读取波长命令！", u"错误提示", wx.OK )as dlg:
                dlg.ShowModal()

        if 'SignalCommand' in config:
            self.SignalCommand  = config['SignalCommand']
        else:
            with wx.MessageDialog(self, u"配置文件没有配置串口读取光强的命令！", u"错误提示", wx.OK )as dlg:
                dlg.ShowModal()